<?php
namespace app\admin\controller;

class Withdraw extends Base
{
    private $tn = 'withdraw';

    public function index()
    {
        if (isset($this->get['page']) && isset($this->get['limit'])) {
            $where = [];
            if(isset($this->get['status'])){
                $where['w.status'] = $this->get['status'];
            }
            if(isset($this->get['date'])){
                $where['w.add_time'] = [['egt',strtotime($this->get['date'])],['lt',strtotime($this->get['date'].'+1 day')]];
            }
            if(session('admin_id') != 1){
                $where['w.admin_id'] = session('admin_id');
            }else{
                if(isset($this->get['admin_id'])){
                    $where['w.admin_id'] = $this->get['admin_id'];
                }
            }
            $list  = db($this->tn)->alias('w')->join(['admin_admin' => 'a'],'a.id = w.admin_id','left')->field('w.*,a.username')->where($where)->order('id', 'desc')->page($this->get['page'])->limit($this->get['limit'])->select();
            foreach ($list as $k => $v) {
                $list[$k]['add_time'] = date('Y-m-d H:i',$v['add_time']);
            }
            $count = db($this->tn)->alias('w')->join(['admin_admin' => 'a'],'a.id = w.admin_id','left')->where($where)->count();
            $this->returnAPI('', 0, $list, ['count' => $count, 'limit' => $this->get['limit']]);
        }
        return $this->fetch('');
    }

    public function edit($id = 0)
    {
        $model = model($this->tn);
        if ($this->post) {
            if(floatval($this->post['price']) <= 0){
                $this->returnAPI('请输入有效的金额');
            }
            $balance = db('admin')->where('id',session('admin_id'))->value('balance');
            if(floatval($this->post['price']) > $balance){
                $this->returnAPI('账户余额不足');
            }
            $this->post['admin_id'] = session('admin_id');
            db()->startTrans();
            $result = $model->validate($this->tn)->allowField(true)->save($this->post);

            if ($result === false) {
                db()->rollback();
                $this->returnAPI($model->getError());
                
            }
            //修改账户余额
            if(!db('admin')->where('id',session('admin_id'))->update([
                'balance' => ['dec',floatval($this->post['price'])],
                'f_balance' => ['inc',floatval($this->post['price'])]
            ])){
                db()->rollback();
                $this->returnAPI('余额修改失败');
            }   
            db()->commit();
            $this->returnAPI('操作成功', 0);
        }
        $model = $model->get($id);
        if (is_null($model)) {
            $data = $this->table_fields($this->tn);
        } else {
            $data = $model->getData();
        }

        return $this->fetch('', ['data' => $data,'price'=>db('admin')->where('id',session('admin_id'))->value('balance') ]);
    }

    //提现审核
    public function check(){
        $validate = new \think\validate([
            'id' => 'require',
            'status' => 'require|in:1,2'
        ],[
            'id.require' => '参数id丢失',
            'status.require' => '参数status丢失',
            'status.in' => '操作类型有误'
        ]);
        if($validate->check($this->post) === false){
            $this->returnAPI($validate->getError());
        }
        $info = model($this->tn)->get($this->post['id']);
        if(is_null($info) || $info['status'] != 0){
            $this->returnAPI('信息有误');
        }
        $this->post['check_time'] = time();
        $data['f_balance'] = ['dec',$info['price']];
        if($this->post['status'] == 2){
            $data['balance'] = ['inc',$info['price']];
        }
        db()->startTrans();
        if(!db('admin')->where('id',$info['admin_id'])->update($data)){
            db()->rollback();
            $this->returnAPI('信息修改失败');
        }
        unset($this->post['id']);
        if(!$info->save($this->post)){
            db()->rollback();
            $this->returnAPI('操作失败，未知错误');
        }
        db()->commit();
        $this->returnAPI('操作成功',0);
    }
}
